# SPDX-License-Identifier: Apache-2.0
"""
remove malware infrastructure

Revision ID: ab536b1853f0
Revises: c5f718cb98ac
Create Date: 2023-05-19 13:46:26.093351
"""

import sqlalchemy as sa

from alembic import op
from sqlalchemy.dialects import postgresql

revision = "ab536b1853f0"
down_revision = "c5f718cb98ac"


def upgrade():
    op.drop_index("ix_malware_verdicts_check_id", table_name="malware_verdicts")
    op.drop_table("malware_verdicts")
    op.drop_table("malware_checks")


def downgrade():
    op.create_table(
        "malware_verdicts",
        sa.Column(
            "id",
            postgresql.UUID(),
            server_default=sa.text("gen_random_uuid()"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "run_date",
            postgresql.TIMESTAMP(),
            server_default=sa.text("now()"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column("check_id", postgresql.UUID(), autoincrement=False, nullable=False),
        sa.Column("file_id", postgresql.UUID(), autoincrement=False, nullable=True),
        sa.Column("project_id", postgresql.UUID(), autoincrement=False, nullable=True),
        sa.Column("release_id", postgresql.UUID(), autoincrement=False, nullable=True),
        sa.Column(
            "classification",
            postgresql.ENUM(
                "threat", "indeterminate", "benign", name="verdictclassification"
            ),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "confidence",
            postgresql.ENUM("low", "medium", "high", name="verdictconfidence"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column("message", sa.TEXT(), autoincrement=False, nullable=True),
        sa.Column(
            "details",
            postgresql.JSONB(astext_type=sa.Text()),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "manually_reviewed",
            sa.BOOLEAN(),
            server_default=sa.text("false"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "reviewer_verdict",
            postgresql.ENUM(
                "threat", "indeterminate", "benign", name="verdictclassification"
            ),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column("full_report_link", sa.VARCHAR(), autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ["check_id"],
            ["malware_checks.id"],
            name="malware_verdicts_check_id_fkey",
            onupdate="CASCADE",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["file_id"],
            ["release_files.id"],
            name="malware_verdicts_file_id_fkey",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["project_id"],
            ["projects.id"],
            name="malware_verdicts_project_id_fkey",
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["release_id"],
            ["releases.id"],
            name="malware_verdicts_release_id_fkey",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name="malware_verdicts_pkey"),
    )
    op.create_index(
        "ix_malware_verdicts_check_id", "malware_verdicts", ["check_id"], unique=False
    )
    op.create_table(
        "malware_checks",
        sa.Column(
            "id",
            postgresql.UUID(),
            server_default=sa.text("gen_random_uuid()"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column("name", postgresql.CITEXT(), autoincrement=False, nullable=False),
        sa.Column("version", sa.INTEGER(), autoincrement=False, nullable=False),
        sa.Column(
            "short_description",
            sa.VARCHAR(length=128),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column("long_description", sa.TEXT(), autoincrement=False, nullable=False),
        sa.Column(
            "check_type",
            postgresql.ENUM("event_hook", "scheduled", name="malwarechecktypes"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "hooked_object",
            postgresql.ENUM(
                "File", "Release", "Project", name="malwarecheckobjecttype"
            ),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "schedule",
            postgresql.JSONB(astext_type=sa.Text()),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "state",
            postgresql.ENUM(
                "enabled",
                "evaluation",
                "disabled",
                "wiped_out",
                name="malwarecheckstate",
            ),
            server_default=sa.text("'disabled'::malwarecheckstate"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "created",
            postgresql.TIMESTAMP(),
            server_default=sa.text("now()"),
            autoincrement=False,
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("id", name="malware_checks_pkey"),
        sa.UniqueConstraint("name", "version", name="malware_checks_name_version_key"),
    )
